//
//  Kylin_NewFeatureCollectionViewController.m
//  01-LotteryBaseFrame
//
//  Created by Kylin on 16/1/6.
//  Copyright © 2016年 kylin. All rights reserved.
//

//#define Kylin_Bounds [UIScreen mainScreen].bounds
#import "Kylin_NewFeatureCollectionViewController.h"
#import "Kylin_NewFeatureCollectionViewCell.h"
@interface Kylin_NewFeatureCollectionViewController()

// guide图片
@property (nonatomic,weak) UIImageView *guide;

// 偏移值
@property (nonatomic,assign) CGFloat lastOffsetX;

// 大标题
@property (nonatomic,weak) UIImageView *guideLargeText;

// 小标题
@property (nonatomic,weak) UIImageView *guideSmallText;

@end
@implementation Kylin_NewFeatureCollectionViewController


// 包装flowlayout 外界一创建就按以下进行设定
- (instancetype)init{
    
   // 1:创建流水布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
   
   // 行间距
    flowLayout.minimumLineSpacing = 0;
    
    // item间距
    flowLayout.minimumInteritemSpacing = 0;
    
    // 设置每个item(cell)的尺寸
    flowLayout.itemSize = Kylin_Bounds.size;
    
    // 设置滚动方向 水平
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [super initWithCollectionViewLayout:flowLayout];
}
static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad{
    

    self.view.backgroundColor = [UIColor redColor];
    
    
    // 注册item(cell) //跟 着更改
    [self.collectionView registerClass:[Kylin_NewFeatureCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self setUp];
    [self setUpAddChildView];
    
}

- (void)setUp{
    
    // 设置分页
    self.collectionView.pagingEnabled = YES;
    
    // 禁止弹簧效果
    self.collectionView.bounces = NO;
    
    // 隐藏滑动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
}

- (void)setUpAddChildView{
    
    // 添加导航线
    UIImageView *guideLine = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLine"]];
    guideLine.x -= 150;
    [self.collectionView addSubview:guideLine];
    
    // 添加图片
    UIImageView *guide = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide1"]];
    guide.x += 50;
    self.guide = guide; // 记录下guide的位置
    [self.collectionView addSubview:guide];
    
    // 3大标题
    UIImageView *guideLargeText = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    [self.collectionView addSubview:guideLargeText];
    guideLargeText.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.7);
    
    self.guideLargeText = guideLargeText;
    // 设置小标题
    UIImageView *guideSmallText = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    [self.collectionView addSubview:guideSmallText];
    guideSmallText.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.8);
    self.guideSmallText = guideSmallText;
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
//     NSLog(@"%f",scrollView.contentOffset.x);
    
    // 总偏差
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 计算偏差
    CGFloat def = offsetX - self.lastOffsetX;
    
  
    // 更换图片
    
    NSInteger page = offsetX / self.view.width + 1;
    
    NSString *name = [NSString stringWithFormat:@"guide%ld",page];
    
    self.guide.image = [UIImage imageNamed:name];
    
    
    // 平移图片 不需要动画每次加上偏差值就可以了,
    self.guide.x += def * 2;
    // 需要动画时,每让图片多出两个偏差值,过一段时间后就减去一个偏差值,就有动画效果
    [UIView animateWithDuration:0.25 animations:^{
        self.guide.x -= def;

    }];
    
    
    
    // 更换大标题
    NSString *name1 = [NSString stringWithFormat:@"guideLargeText%ld",page];
    
    self.guideLargeText.image = [UIImage imageNamed:name1];
    // 大标题动画效果
    self.guideLargeText.x += def * 2;
    [UIView animateWithDuration:0.25 animations:^{
        self.guideLargeText.x -= def;
    }];
    
    
    // 更换小标题
    NSString *name2 = [NSString stringWithFormat:@"guideSmallText%ld",page];
    
    self.guideSmallText.image = [UIImage imageNamed:name2];
    // 小题动画效果
    self.guideSmallText.x += def * 2;
    [UIView animateWithDuration:0.25 animations:^{
        self.guideSmallText.x -= def;
    }];
    
    
    self.lastOffsetX = offsetX;
    

}
#define Kylin_page 4
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return Kylin_page;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    Kylin_NewFeatureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    // 通过item cell 控制图片的变化
    NSString *name = [NSString stringWithFormat:@"guide%ldBackground",indexPath.item + 1];
    UIImage *image = [UIImage imageNamed:name];
    cell.image = image;
    
    // 计算是否最后一页,
    [cell setIndexPath:indexPath count:Kylin_page];
    return cell;
}
@end
